data structures math *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

int t, n;
int fwk[500005];
bool vis[500005], yes = 0;

void solve() {
    cin >> n; yes = 0;
    for(int i=1;i<=n;i++) vis[i] = 0;
    int i = 1;
    vector<pair<int, int>> vec(n);
    for(auto &e:vec) {
        cin >> e.first; e.second = i++;
        if(vis[e.first]) {yes = 1;}
        vis[e.first] = 1;
    }
    if(yes) {cout << "YES\n"; return ;}
    sort(vec.rbegin(), vec.rend());
    bool res=0;
    for(auto e:vec) {
        for(int cx=e.second;cx;cx-=cx&-cx)
            res ^= fwk[cx];
        for(int cx=e.second;cx<=n;cx+=cx&-cx)
            fwk[cx] ^= 1;
    }
    for(int i=1;i<=n;i++) fwk[i] = 0;
    cout << (res ? "NO\n" : "YES\n");
}

int main() {
    cin.tie(0) -> sync_with_stdio(0);
    cin>>t;
    while(t--) solve();
}


Comments

Submit
0 Comments
More Questions

1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety